package cn.smxy.ln.controller;

import cn.smxy.ln.common.lang.Result;
import cn.smxy.ln.common.utils.RedisUtil;
import cn.smxy.ln.pojo.AdminType;
import cn.smxy.ln.pojo.User;
import cn.smxy.ln.service.AdminTypeService;
import cn.smxy.ln.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ln
 * @date 2022年05月24日 19:07
 */
@RestController
@RequestMapping("/admintype")
public class AdminTypeController {

    @Autowired
    AdminTypeService adminTypeService;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    @GetMapping("/list")
    @Cacheable(cacheNames = "adminType", unless = "#result.getData() == null")    //key:缓存数据时使用的 key，可以用它来指定。默认是使用方法参数的值；cacheNames/value：二者选其一即可，指定缓存组件的名字
    @HystrixCommand(fallbackMethod = "hystrixGetList")
    public Result queryAll() {
        List<AdminType> list = adminTypeService.list();
        if (list == null) {
            throw new RuntimeException("数据库没有admintype数据，或者信息无法找到");
        }
        return Result.succ(list);
    }

    public Result hystrixGetList() {
        return Result.fail("没有得到对应的信息，数据库admintype是空的，或者刷新试试");
    }

    @PostMapping("/save")
    @Transactional
    public Result save(@RequestBody AdminType adminType) {
        if ((adminType.getName() == null || adminType.getName().equals("")) && (adminType.getMemo() == null || adminType.getMemo().equals("")))
            return Result.fail("adminType为空，保存失败");
        if (adminType.getName() == null || adminType.getName().equals(""))
            return Result.fail("name不能为空，保存失败");
        boolean flag = adminTypeService.save(adminType);
        if (flag) {
            redisUtil.flushByX("adminType");
            return Result.succ(adminType);
        }
        return Result.fail("保存失败");
    }

    @PostMapping("/delete")
    @Transactional // 该方法支持事务
    public Result delete(@RequestParam Long id) {
        List<User> list = userService.list();
        for (User u: list) {
            String[] st = u.getAdminType().split(",");
            String t = "";
            for (int i = 0; i < st.length; i++) {
                if (Long.valueOf(st[i]) != id) {
                    if (t.equals("")) {
                        t += st[i];
                    } else {
                        t += "," + st[i];
                    }
                }
            }
            u.setAdminType(t);
            userService.updateById(u);
        }
        boolean flag = adminTypeService.removeById(id);
        if (flag) {
            redisUtil.flushByX("adminType");
            return Result.succ("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    @Transactional
    public Result update(@RequestBody AdminType adminType) {
        if ((adminType.getName() == null || adminType.getName().equals("")) && (adminType.getMemo() == null || adminType.getMemo().equals("")))
            return Result.fail("adminType为空，更新失败");
        if (adminType.getName() == null || adminType.getName().equals(""))
            return Result.fail("name不能为空，更新失败");
        boolean flag = adminTypeService.updateById(adminType);
        if (flag) {
            redisUtil.flushByX("adminType");
            return Result.succ(adminType);
        }
        return Result.fail("修改失败");
    }
}
